package editor
{
	import Classes.pi_events;
	import Classes.pi_images;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_popupMenu;
	import UIFree.uif_component;
	
	import cardsbody.*;
	
	import dialogs.pi_alert;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;

	public class pi_toolbar extends pi_baseToolbar {
	    
	    private var c_pay: pi_toolbarButton;
	    private var c_balance: pi_toolbarButton;
		private	var eyeFull: pi_toolbarButton;
		private	var link: pi_toolbarButton;
		private	var tofile: pi_toolbarButton;
		private var effect: pi_toolbarButton;
	    
		public function pi_toolbar(parent:uif_component, startPos: int, align: String = 'bottom', a_paySupport: Boolean = true) {
			super(parent, startPos, align);
			
// Кнопка "Полный размер"			
			eyeFull = new pi_toolbarButton(this, pi_images.c_eye, pi_data.FULLVIEWCARD, function(e: MouseEvent): void {
				app.dispatchEvent(new Event(pi_events.EYERELEASE));
			}, 72);

// Кнопка "Входящие"
			var inbox_button: pi_toolbarButton = new pi_toolbarButton(this, pi_images.c_inbox, pi_data.INBOX, function(e: MouseEvent): void {
				if (!inbox_button.disable) app.dispatchEvent(new Event(pi_events.INBOXRELEASE));
			}, 85);
			inbox_button.disable = true;
			
			app.addEventListener(pi_events.CHECKBOX, function(e: ObjectEvent): void {
				inbox_button.disable  = false;//parseInt(e.object.inCount) == 0;
				outbox_button.disable = false;//parseInt(e.object.isOut) == 0;
				if (parseInt(e.object.receiveCount) < parseInt(e.object.inCount)) inbox_button.blink();
			});

// Кнопка "Исходящие"
			var outbox_button: pi_toolbarButton = new pi_toolbarButton(this, pi_images.c_outbox, pi_data.OUTBOX, function(e: MouseEvent): void {
				if (!outbox_button.disable) app.dispatchEvent(new Event(pi_events.OUTBOXRELEASE));
			}, 93);
			outbox_button.disable = true;
			
// Кнопка "эффекты"
			effect = new pi_toolbarButton(this, pi_images.c_effect, pi_data.EFFECTS, function(e: MouseEvent): void {
				app.dispatchEvent(new Event(pi_events.SHOWEFFECTDIALOG));
			}, 85);
			effect.visible = false;
			

// Кнопка "Сохранить в файл"
/*
			function saveToJPG(cardbody: Object): void {
				var a_alert: pi_alert  = pj.alert(pi_data.MESSAGE, pi_data.JPGWAIT, null, false, null, function(): void {
					var a_bytes: ByteArray = cardbody.asBytes();
					a_alert.close(0, function(): void {
						pj.manager.saveJPG(a_bytes);
					});
				});
				a_alert.okButton.visible = false;
			}*/

			function toFileSave(e: Object=null): void {
				import MD5;
				var cardbody: Object = pj.isFullView?pj.fullScreen:pj.cardbody;
				if (cardbody.hasOwnProperty('asBytes')) pj.saveToJPG(cardbody);
				else pj.dispatchEvent(new Event(pi_events.COLLECTFILERELEASE));
			}
			tofile = new pi_toolbarButton(this, pi_images.c_tofile, pi_data.COLLECTFILE, toFileSave, 90);
			tofile.visible = false;
			
			pj.addEventListener(pi_events.FULLVIEWPHOTOCOMPLETE, function(e: Event): void {
				if ((pj.editor.cardBody.maskInfo.price == 0) || 
					(pj.editor.cardBody.maskInfo.id != pj.fullScreen.maskInfo.id)) tofile.slideShow();
			})
			
// При загрузке фото в редакторе
			app.addEventListener(pi_events.EDITORPHOTOLOADCOMPLETE, function(e: ObjectEvent): void {
				if (!pj.editor.cardBody.maskInfo.price) {
//					if (!pj.isFullView) sendButtonsShow(true);
					tofile.slideShow();
				} else tofile.slideHide();
			});

			app.addEventListener(pi_events.COMPLETELOADCARD, function(e: ObjectEvent): void {
//				sendButtonsShow((pj.editor.cardBody.maskInfo.price == 0) && pj.isPhotoLoaded && !pj.isFullView);
				refrechVisibleEffect();
			});

			app.addEventListener(pi_events.CLEARPRICE, function(e: ObjectEvent): void {
				if (pj.isPhotoLoaded && 
					((e.object as Array).indexOf(pj.editor.cardBody.maskInfo.id) > -1)) {
					tofile.slideShow();
//					sendButtonsShow(true);
				}
			});
			
/*			app.addEventListener(pi_events.CARDSELECT, function(e: ObjectEvent): void { 
				// Если пользователь выбирает входящую открытку
				// Тогда скрываем кнопку "Отправить"
				sendButtonsShow(false);
			});*/

// Кнопка, "получить ссылку"
/*			link = new pi_toolbarButton(this, pi_images.c_link, pi_data.GETLINK, function(e: MouseEvent): void {
				pj.dispatchEvent(new Event(pi_events.LINKRELEASE));
			}, 100);
			link.visible = false;*/
			
			if (a_paySupport) paySupport();
			
			refreshButtons();
		}
		
		protected function refrechVisibleEffect(): void {
			if (pj.isFullView) effect.slideHide();
			else {
				if (pj.editor.cardBody is pi_pngcard) effect.slideShow();
				else effect.slideHide();
			}
		}
		
/*		protected function sendButtonsShow(isShow: Boolean): void {
			if (isShow) {
				link.slideShow();
			} else {
				link.slideHide();
			}
		}*/
		
		protected function doBalanceMenu(e: ObjectEvent): void {
			if (e.object.event is Function) (e.object.event as Function)()
			else pj.dispatchEvent(new ObjectEvent(e.object.event, e.object));
		}
		
		override protected function showFullView(): void {
			super.showFullView();
			refrechVisibleEffect();
			if (!pj.isFullView) {	// Если не на полный экран
				eyeFull.text = pi_data.FULLVIEWCARD;
				if (pj.editor.cardBody.maskInfo.price) tofile.slideHide();
//				else sendButtonsShow(true);
			} else {
				eyeFull.text = pi_data.FULLVIEWCARDCANCEL;
//				sendButtonsShow(false);
			}
		}
		
		private function stateEditAndPrice(): Boolean {
			return !pj.isFullView && (pj.editor.cardBody.maskInfo.price > 0);
		}
		
		override protected function updateBounds():uif_component {
			var res: uif_component = super.updateBounds();
			refreshButtons();
			return res;
		}
			
		protected function paySupport(): void {
// Кнопка баланса
			var menu: Array = pj.balanceMenu;
			c_balance = new pi_toolbarButton(this, pi_images.c_ibalance, pi_data.ADDBALANCE, function(e: MouseEvent): void {
				pj.payDialog();
			}, 100, 'right');
			c_balance.visible = true;
			
			if (menu.length > 0) {
				var submenu: uif_popupMenu =  new uif_popupMenu(pj.topLayer, {
					orientation	: uif_popupMenu.TOPPOSITION,
					itemClass	: pj.classes.button,
					showEvent	: MouseEvent.ROLL_OVER
				}, 100, 100, 'popupmenu');
				
				submenu.listMenu = menu;
				
				submenu.activeControl = c_balance;
				submenu.addEvent(Event.SELECT, doBalanceMenu);
			}
			
			app.addEventListener(pi_events.CHANGEBALANCE, function(e: ObjectEvent): void {
				if (e.object > 0) {
					c_balance.text = pi_data.BALANCE + '\n' + 
									String(Math.round(parseFloat(String(e.object)) * 100) / 100) + 
									' ' + pj.siteData.CURRENCY;
//					c_balance.slideShow();
				} else c_balance.text = pi_data.ADDBALANCE;
			});
// Кнопка плохой оплаты			
			var c_paybad: pi_toolbarButton = new pi_toolbarButton(this, pi_images.c_paybad, '', function(e: MouseEvent): void {
				pj.alert(pi_data.ERROR, pi_data.PAYBADTEXT);
			}, 100, 'right');
			c_paybad.text = pi_data.PAYBADLABEL;
			c_paybad.visible = false;
			app.addEventListener(pi_events.PAYBAD, function(e: Event): void {
				c_paybad.slideShow();
			});

// Кнопка цены
			c_pay = new pi_toolbarButton(this, pi_images.c_price, '', function(e: MouseEvent): void {
				pj.dispatchEvent(new GDataEvent(pi_events.PAYCARDRELEASE, pj.editor.cardBody.maskInfo.id));
			}, 100, 'right');
			c_pay.visible = false;
			
			app.addEventListener(pi_events.CLEARPRICE, function(e: ObjectEvent): void {
				if (pj.editor.cardBody && ((e.object as Array).indexOf(pj.editor.cardBody.maskInfo.id) > -1))
					c_pay.slideHide();
			});
			
			app.addEventListener(pi_events.COMPLETELOADCARD, function(e: ObjectEvent): void {
				var card: pi_cardbody = e.object as pi_cardbody;
				if (card != pj.fullScreen) {
					c_pay.text = '';
					if (card.maskInfo.price) {
						c_pay.slideShow();
						c_pay.text = pi_data.PRICE + '\n' + card.maskInfo.price + ' ' + pj.siteData.CURRENCY;
					} else c_pay.slideHide();
				} else c_pay.slideHide();
			});
			
			app.addEventListener(pi_events.FULLVIEWCHANGE, function(e: Event): void {
				if (stateEditAndPrice()) c_pay.slideShow();
				if (pj.isFullView) {
					c_pay.slideHide();
					c_balance.slideHide();
				} else {
					c_balance.slideShow();
					if (stateEditAndPrice()) c_pay.slideShow();
				}
			});
			
		}
		
	}
}